<template>
  <basic-layout title="扫一扫记录" class="scan-layout" showBack>
    <me-scroll
      ref="meScrollRef"
      v-bind="meScrollOptions"
      @init="onMeScrollInit"
      @down-callback="downCallback"
      @up-callback="upCallback"
    >
      <div class="scan-list">
        <div
          v-for="(item, index) in list"
          :key="item.id"
          class="scan-item"
          :data-aos="index % 2 === 0 ? 'fade-right' : 'fade-left'"
          :data-aos-delay="index * 100"
        >
          <div class="scan-content">
            <div class="scan-header">
              <van-image
                round
                width="40"
                height="40"
                :src="item.avatar"
                class="avatar"
              />
              <div class="user-info">
                <span class="username">{{ item.userName }}</span>
                <span class="time">{{ formatTime(item.createdAt) }}</span>
              </div>
            </div>

            <div class="scan-body">
              <p class="content">{{ item.content }}</p>

              <!-- 图片网格 -->
              <div v-if="item.images?.length" class="image-grid" :class="`count-${item.images.length}`">
                <van-image
                  v-for="(img, imgIndex) in item.images"
                  :key="imgIndex"
                  :src="img"
                  fit="cover"
                  class="grid-image"
                  @click="previewImage(item.images, imgIndex)"
                />
              </div>

              <div class="location" v-if="item.location">
                <van-icon name="location-o" />
                <span>{{ item.location }}</span>
              </div>
            </div>

            <div class="scan-footer">
              <van-button
                size="small"
                icon="like-o"
                :class="{ liked: item.isLiked }"
                @click="toggleLike(item)"
              >
                {{ item.likeCount }}
              </van-button>
              <van-button
                size="small"
                icon="comment-o"
              >
                {{ item.commentCount }}
              </van-button>
            </div>
          </div>
        </div>
      </div>
    </me-scroll>
  </basic-layout>
</template>

<script setup lang="ts">
import { onMounted } from 'vue'
import { showImagePreview } from 'vant'
import dayjs from 'dayjs'
import relativeTime from 'dayjs/plugin/relativeTime'
import 'dayjs/locale/zh-cn'
import { Api } from '@/api'
import type { MomentItem } from '@/api'
import MeScroll from '@/components/common/MeScroll.vue'
import useMeScroll from '@/hooks/useMeScroll'
import AOS from 'aos'
import 'aos/dist/aos.css'

dayjs.extend(relativeTime)
dayjs.locale('zh-cn')

// 使用 useMeScroll hook
const {
  meScrollRef,
  list,
  meScrollOptions,
  downCallback,
  upCallback,
} = useMeScroll<MomentItem>({
  requestData: async (params) => {
    const res = await Api.getMoments(params)
    return {
      list: res.data.list,
      total: res.data.pagination.total
    }
  },
  params: { pageSize: 10 }
})

// 格式化时间
const formatTime = (time: string) => {
  return dayjs(time).fromNow()
}

// 预览图片
const previewImage = (images: string[], startPosition: number) => {
  showImagePreview({
    images,
    startPosition,
    closeable: true,
  })
}

// 点赞功能
const toggleLike = (item: MomentItem) => {
  item.isLiked = !item.isLiked
  item.likeCount += item.isLiked ? 1 : -1
}

// MeScroll 初始化回调
const onMeScrollInit = (mescroll: any) => {
  console.log('MeScroll initialized:', mescroll)
}

onMounted(() => {
  AOS.init({
    duration: 800,
    once: true
  })
})
</script>

<style lang="less" scoped>
.scan-layout {
  min-height: 100vh;
  background: #f7f8fa;

  .scan-list {
    padding: 12px;

    .scan-item {
      margin-bottom: 12px;
      animation: slideIn 0.3s ease-out;

      .scan-content {
        background: #fff;
        border-radius: 12px;
        padding: 16px;
        box-shadow: 0 2px 8px rgba(0, 0, 0, 0.04);
        transition: all 0.3s ease;

        &:hover {
          transform: translateY(-2px);
          box-shadow: 0 4px 12px rgba(0, 0, 0, 0.08);
        }

        .scan-header {
          display: flex;
          align-items: center;
          margin-bottom: 12px;

          .avatar {
            margin-right: 12px;
            border: 1px solid #eee;
          }

          .user-info {
            flex: 1;
            display: flex;
            flex-direction: column;
            gap: 4px;

            .username {
              font-size: 16px;
              font-weight: 500;
              color: #333;
            }

            .time {
              font-size: 12px;
              color: #999;
            }
          }
        }

        .scan-body {
          .content {
            font-size: 15px;
            line-height: 1.6;
            color: #333;
            margin-bottom: 12px;
          }

          .image-grid {
            display: grid;
            gap: 8px;
            margin-bottom: 12px;

            &.count-1 .grid-image {
              width: 240px;
              height: 240px;
            }

            &.count-2, &.count-4 {
              grid-template-columns: repeat(2, 1fr);
              .grid-image {
                height: 160px;
              }
            }

            &.count-3, &.count-5, &.count-6, &.count-7, &.count-8, &.count-9 {
              grid-template-columns: repeat(3, 1fr);
              .grid-image {
                height: 120px;
              }
            }

            .grid-image {
              border-radius: 8px;
              overflow: hidden;
              transition: transform 0.3s ease;

              &:hover {
                transform: scale(1.02);
              }
            }
          }

          .location {
            display: flex;
            align-items: center;
            gap: 4px;
            color: #999;
            font-size: 12px;
            margin-top: 8px;
          }
        }

        .scan-footer {
          display: flex;
          gap: 12px;
          margin-top: 16px;
          padding-top: 12px;
          border-top: 1px solid #f5f5f5;

          .van-button {
            padding: 0 16px;
            border-radius: 16px;
            color: #666;
            background: #f8f8f8;
            border: none;
            transition: all 0.3s ease;

            &:active {
              transform: scale(0.95);
            }

            &.liked {
              color: #ee0a24;
              background: rgba(238, 10, 36, 0.1);
            }
          }
        }
      }
    }
  }
}

@keyframes slideIn {
  from {
    opacity: 0;
    transform: translateY(20px);
  }
  to {
    opacity: 1;
    transform: translateY(0);
  }
}
</style>
